package view;

import java.awt.Font;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

import panels.CanvasLinePanel;
import panels.SPanel;

import jpcap.packet.Packet;

import model.App;

public class SummaryFrame extends JFrame {
	private static final long serialVersionUID = -7095993976674315074L;
	private int width = 600;
	private int height = 550;
	private List<Packet> ptList = null;
	private SPanel mainPanel = new SPanel();
	private JLabel line1 = new JLabel();
	private JLabel line2 = new JLabel();
	private JLabel line3 = new JLabel();
	private Font lineFont = new Font(null, Font.PLAIN, 25);

	public SummaryFrame() {
		ptList = App.packetList;
		initUI();
		initListener();
		setTitle("SirusSniffer--Summary");
		setSize(width, height);
		setLocationRelativeTo(null);
		setVisible(true);
	}

	private void initUI() {
		JPanel clp = null;
		if (ptList != null && ptList.size() > 0) {
			long flowcount = 0;
			long flowtime = ptList.get(0).sec;
			List<Long> flow = new ArrayList<Long>();
			for (Packet pt : ptList) {
				flowcount += pt.len;
				if (Math.abs(pt.sec - flowtime) >= 1000) {
					flowtime = pt.sec;
					flow.add(flowcount);
				}
			}
			line1.setText("总共捕获 " + ptList.size() + "个有效数据包");
			line2.setText("总计流量 " + (flowcount / 1024) + "KB");
			line3.setText("总计用时 " + App.packetTime + "毫秒");
			clp = new CanvasLinePanel();
		} else {
			line1.setText("总共捕获 0个有效数据包");
			line2.setText("总计流量 0个字节");
			line3.setText("总计用时 " + App.packetTime + "毫秒");
			clp = new JPanel();
		}
		line1.setFont(lineFont);
		line2.setFont(lineFont);
		line3.setFont(lineFont);
		add(mainPanel);
		mainPanel.add(line1);
		mainPanel.add(line2);
		mainPanel.add(line3);
		mainPanel.add(clp);
		mainPanel.setSize(width - 10, height - 10);
		mainPanel.setCss(10, (new int[] { 15, 15, 15 ,50}));

	}

	private void initListener() {

	}

	public static void main(String[] args) throws Exception {
		UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		new SummaryFrame();
	}
}
